from django.core.exceptions              import ValidationError
from sitepal.sitetheme.models.containers import GRID_CUSTOM_CHOICES,GRID_CLEAR_CHOICES
from sitepal.sitetheme.models.containers import GRID_POSITION_CHOICES,GRID_VISIBILITY_CHOICES
from sitepal.sitetheme.models.containers import GRID_MIN_PREFIX,GRID_MAX_PREFIX
from sitepal.sitetheme.models.containers import GRID_MIN_SUFFIX,GRID_MAX_SUFFIX
from sitepal.sitetheme.models.containers import GRID_MIN_WEIGHT,GRID_MAX_WEIGHT
from sitepal.sitetheme.models.containers import GRID_MIN_WIDTH,GRID_MAX_WIDTH

def validate_grid_overrides(value):
    def map_choice(choice):
        if isinstance(choice,(list,tuple)):
            return choice[0]
        return choice
    def validate_integer(value,name):
        if not isinstance(value[name],(int,long)):
            raise ValidationError,u"{0} is not an integer".format(name)
    def validate_range(value,name,min_value,max_value):
        value = value[name]
        if not (min_value <= value <= max_value):
            raise ValidationError,u"{0} must be in the range {1}..{2}".format(name,min_value,max_value)
    def validate_string(value,name):
        if not isinstance(value[name],basestring):
            raise ValidationError,u"{0} is not a string".format(name)
    def validate_choices(value,name,choices):
        choices = map(map_choice,choices)
        if not value[name] in choices:
            raise ValidationError,u"{0} must be one of {1}".format(name,choices)
    def validate_bool(value,name):
        if not isinstance(value[name],bool):
            raise ValidationError,u"{0} is not a boolean".format(name)
    if value:
        if 'weight' in value:
            validate_integer(value,'weight')
            validate_range  (value,'weight',-100,100)
        if 'grid_custom' in value:
            validate_string (value,'grid_custom')
            validate_choices(value,'grid_custom',GRID_CUSTOM_CHOICES)
        if 'grid_clear' in value:
            validate_string (value,'grid_clear')
            validate_choices(value,'grid_clear',GRID_CLEAR_CHOICES)
        if 'grid_enabled' in value:
            validate_bool(value,'grid_enabled')
        if 'grid_pages' in value:
            validate_string (value,'grid_pages')
        if 'grid_position' in value:
            validate_string (value,'grid_position')
            validate_choices(value,'grid_position',GRID_POSITION_CHOICES)
        if 'grid_prefix' in value:
            validate_integer(value,'grid_prefix')
            validate_range  (value,'grid_prefix',GRID_MIN_PREFIX,GRID_MAX_PREFIX)
        if 'grid_suffix' in value:
            validate_integer(value,'grid_suffix')
            validate_range  (value,'grid_suffix',GRID_MIN_SUFFIX,GRID_MAX_SUFFIX)
        if 'grid_weight' in value:
            validate_integer(value,'grid_weight')
            validate_range  (value,'grid_weight',GRID_MIN_WEIGHT,GRID_MAX_WEIGHT)
        if 'grid_width' in value:
            validate_integer(value,'grid_width')
            validate_range  (value,'grid_width',GRID_MIN_WIDTH,GRID_MAX_WIDTH)
        if 'grid_visibility' in value:
            validate_string (value,'grid_visibility')
            validate_choices(value,'grid_visibility',GRID_VISIBILITY_CHOICES)

